package com.example.yiparkapplication.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSONObject;
import com.example.yiparkapplication.R;
import com.example.yiparkapplication.constant.API;
import com.example.yiparkapplication.tool.FastPay;
import com.example.yiparkapplication.tool.IAlPayResultListener;
import com.example.yiparkapplication.util.BaseActivity;
import com.example.yiparkapplication.util.Common;
import com.rey.material.widget.Button;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

//AppCompatActivity
public class PayActivity extends BaseActivity implements View.OnClickListener, IAlPayResultListener {

    private static String mTradingRecordId;
    private static String mPrice;
    private static Double mSingleCarPrice;
    private static String mFlag;//微信支付，支付宝支付
    private static int mType;//交易类型(0:车单支付 3:买优惠卡 4:多单支付 5:余额充值)

    public static void getPayInfo(String tradingRecord, String price, String flag, int type, Double singleCarPrice){
        mTradingRecordId = tradingRecord;
        mPrice = price;
        mFlag = flag;
        mType = type;
        mSingleCarPrice = singleCarPrice;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_info_layout);

        TextView paymentDescriptionT = findViewById(R.id.id_payment_description);
        String description = "";
        switch(mType){
            case 0:
                description = "车单付款";
                break;
            case 3:
                description = "购买优惠卡";
                break;
            case 4:
                description = "购买预付";
                break;
        }
        paymentDescriptionT.setText(description);
        TextView modeOfPaymentT = findViewById(R.id.id_mode_of_payment);
        if (mFlag.equals("alipay")){
            modeOfPaymentT.setText("支付宝");
        }else if (mFlag.equals("wx")){
            modeOfPaymentT.setText("微信");
        }
        TextView priceT = findViewById(R.id.id_price);
        priceT.setText(mPrice + "元");
        Button confirmPaymentB = findViewById(R.id.id_confirm_payment);
        confirmPaymentB.setOnClickListener(this);
    }

    @Override
    public void onClick(View v){
        switch(v.getId()){
            case R.id.id_confirm_payment:
                onPay(mTradingRecordId);
        }
    }

    private void onPay(String tradingRecordId){
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(API.HOST + API.URL_PAY + "?tradingRecordId=" + tradingRecordId + "&price=" + mSingleCarPrice + "&openId" + "")
                .build();
        okHttpClient.newCall(request).enqueue(new Callback(){
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e){
                Common.failureMessage(e.getMessage(), getApplicationContext());
            }
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException{
                String tempResponse = response.body().string();
                JSONObject joOut = Common.tempResponseException(tempResponse, getApplicationContext());
                int code = joOut.getIntValue("code");
                if (code == 100){
                    if (mFlag.equals("alipay")){
                        final String paySign = joOut.getJSONObject("object").getString("AliPayURL");
                        FastPay.create(PayActivity.this).setPayResultListener(PayActivity.this).alPay(paySign);
                    }else if (mFlag.equals("wx")){
                        Looper.prepare();
                        Toast.makeText(getApplicationContext(), "暂不支持微信支付", Toast.LENGTH_LONG).show();
                        Looper.loop();
                    }
                }
            }
        });
    }

//    void onPaySuccess();
//    void onPaying();
//    void onPayFail();
//    void onPayCancel();
//    void onPayConnectError();
    @Override
    public void onPaySuccess(){
        Toast.makeText(getApplicationContext(), "付款成功", Toast.LENGTH_LONG).show();
        SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
        editor.putBoolean("paySuccess", true);
        editor.apply();
        finish();
    }

    @Override
    public void onPaying(){
        Toast.makeText(getApplicationContext(), "正在付款", Toast.LENGTH_LONG).show();
        finish();
    }

    @Override
    public void onPayFail(){
        Toast.makeText(getApplicationContext(), "付款失败", Toast.LENGTH_LONG).show();
        finish();
    }

    @Override
    public void onPayCancel(){
        Toast.makeText(getApplicationContext(), "付款已取消", Toast.LENGTH_LONG).show();
        finish();
    }

    @Override
    public void onPayConnectError(){
        Toast.makeText(getApplicationContext(), "付款链接错误", Toast.LENGTH_LONG).show();
        finish();
    }
//    这个不知道是啥
//    @Override
//    public boolean onKeyDown(int keyCode, keyEvent event){
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            finish();
//        }
//        return true;
//    }

}
